package com.nanjia.boot.delivery.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.certificatetype.model.entity.CertificateType;
import com.nanjia.boot.certificatetype.model.query.CertificateTypeQuery;
import com.nanjia.boot.certificatetype.service.CertificateTypeService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.delivery.model.form.CertificateForm;
import com.nanjia.boot.delivery.model.query.CertificateQuery;

import com.nanjia.boot.delivery.model.vo.CertificateVO;

import com.nanjia.boot.delivery.service.CertificateService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 证件前端控制层
 *
 * @author 周洋
 * @since 2025-06-13 11:09
 */
@Tag(name = "证件接口")
@RestController
@RequestMapping("/api/v1/certificate")
@RequiredArgsConstructor
public class CertificateController  {

    private final CertificateService certificateService;

    private final CertificateTypeService certificateTypeService;

    @Operation(summary = "证件分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:query')")
    public PageResult<CertificateVO> getCertificatePage(CertificateQuery queryParams ) {
        IPage<CertificateVO> result = certificateService.getCertificatePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增证件")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:add')")
    public Result<Void> saveCertificate(@RequestBody @Valid CertificateForm formData, HttpServletRequest request ) {
        boolean result = certificateService.saveCertificate(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取证件表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:edit')")
    public Result<CertificateForm> getCertificateForm(
        @Parameter(description = "证件ID") @PathVariable Long id
    ) {
        CertificateForm formData = certificateService.getCertificateFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改证件")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:edit')")
    public Result<Void> updateCertificate(
            @Parameter(description = "证件ID") @PathVariable Long id,
            @RequestBody @Validated CertificateForm formData
    ) {
        boolean result = certificateService.updateCertificate(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除证件")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:delete')")
    public Result<Void> deleteCertificates(
        @Parameter(description = "证件ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = certificateService.deleteCertificates(ids);
        return Result.judge(result);
    }


    @Operation(summary = "证件类型下拉框")
    @GetMapping("/getCertificateTypeList")
    // @PreAuthorize("@ss.hasPerm('certificate:certificate:query')")
    public Result<List<CertificateType>> getCertificateTypeList(CertificateTypeQuery queryParams) {
        List<CertificateType> result = certificateTypeService.getCertificateTypeList(queryParams);
        return Result.success(result);
    }


}

